﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using SisCRA.Classes;
using SisCRA.Database;

namespace SisCRA.Business
{
    public class StudentEnrollmentBLL
    {
        #region -Public Methods-
        
        /// <summary>
        /// Método que chama a camada de dados para matricular um aluno em uma disciplina.
        /// </summary>
        /// <param name="studentEnrollment">Objeto Matrícula</param>
        public void EnrollStudent(StudentEnrollment studentEnrollment)
        {
            try
            {
                StudentEnrollmentDAL studentEnrollmentDAL = new StudentEnrollmentDAL();
                studentEnrollmentDAL.InsertStudentDiscipline(studentEnrollment.StudentId, studentEnrollment.DisciplineId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Método que chama a camada de dados para desmatricular um aluno de uma disciplina.
        /// </summary>
        /// <param name="studentEnrollment">Objeto Matrícula</param>
        public void CancelEnrollStudent(StudentEnrollment studentEnrollment)
        {
            try
            {
                StudentEnrollmentDAL studentEnrollmentDAL = new StudentEnrollmentDAL();
                studentEnrollmentDAL.DeleteStudentDiscipline(studentEnrollment.StudentId, studentEnrollment.DisciplineId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Método que chama a camada de dados para atualizar a quantidade de faltas de um aluno em uma disciplina.
        /// </summary>
        /// <param name="studentEnrollment">Objeto Matrícula</param>
        public void SaveStudentFaults(StudentEnrollment studentEnrollment)
        {
            try
            {
                StudentEnrollmentDAL studentEnrollmentDAL = new StudentEnrollmentDAL();
                studentEnrollmentDAL.UpdateStudentFaults(studentEnrollment.StudentId, studentEnrollment.DisciplineId, studentEnrollment.Faults);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Método que chama a camada de dados para atualizar a média final e a situação de um aluno em uma disciplina.
        /// </summary>
        /// <param name="studentEnrollment">Objeto Matrícula</param>
        public void SaveStudentFinalGradeAndSituation(StudentEnrollment studentEnrollment)
        {
            try
            {
                DisciplineDAL disciplineDAL = new DisciplineDAL();
                DataTable dtDiscipline = disciplineDAL.GetDisciplineById(studentEnrollment.DisciplineId);

                StudentEnrollmentDAL studentEnrollmentDAL = new StudentEnrollmentDAL();
                DataTable dtStudentEnrollment = studentEnrollmentDAL.GetStudentEnroll(studentEnrollment.StudentId, studentEnrollment.DisciplineId);

                int maxFaults = int.Parse(dtDiscipline.Rows[0]["MAXIMO_FALTAS_PERMITIDAS"].ToString());
                int faults = int.Parse(dtStudentEnrollment.Rows[0]["FALTAS"].ToString());

                if (faults > maxFaults || studentEnrollment.FinalGrade < 4)
                {
                    studentEnrollment.Situation = "REPROVADO";
                }
                else if (studentEnrollment.FinalGrade < 6)
                {
                    studentEnrollment.Situation = "EXAME FINAL";
                }
                else
                {
                    studentEnrollment.Situation = "APROVADO";
                }

                studentEnrollmentDAL.UpdateStudentFinalGradeAndSituation(studentEnrollment.StudentId, studentEnrollment.DisciplineId, studentEnrollment.FinalGrade, studentEnrollment.Situation);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Método que chama a camada de dados para buscar um aluno matriculado em uma disciplina.
        /// </summary>
        /// <param name="studentEnrollment">Objeto Matrícula</param>
        /// <returns>DataTable contendo a busca realizada</returns>
        public DataTable GetStudentEnroll(StudentEnrollment studentEnrollment)
        {
            try
            {
                StudentEnrollmentDAL studentEnrollmentDAL = new StudentEnrollmentDAL();
                return studentEnrollmentDAL.GetStudentEnroll(studentEnrollment.StudentId, studentEnrollment.DisciplineId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Método que chama a camada de dados para buscar todos os alunos que não estão matriculados nessa disciplina.
        /// </summary>
        /// <param name="discipline">Objeto Disciplina</param>
        /// <returns>DataTable contendo a busca realizada</returns>
        public DataTable GetStudentNotEnroll(Discipline discipline)
        {
            try
            {
                StudentEnrollmentDAL studentEnrollmentDAL = new StudentEnrollmentDAL();
                return studentEnrollmentDAL.GetStudentNotEnroll(discipline.Id);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Método que chama a camada de dados para buscar todos os alunos dessa disciplina.
        /// </summary>
        /// <param name="discipline">Objeto Disciplina</param>
        /// <returns>DataTable contendo a busca realizada</returns>
        public DataTable GetAllStudentByDiscipline(Discipline discipline)
        {
            try
            {
                StudentEnrollmentDAL studentEnrollmentDAL = new StudentEnrollmentDAL();
                return studentEnrollmentDAL.GetStudentByDiscipline(discipline.Id);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Método que chama a camada de dados para buscar os dados final de uma disciplina.
        /// </summary>
        /// <param name="studentEnrollment">Objeto Matrícula</param>
        /// <returns>DataTable contendo a busca realizada</returns>
        public DataTable GetEnrollmentByDiscipline(StudentEnrollment studentEnrollment)
        {
            try
            {
                StudentEnrollmentDAL studentEnrollmentDAL = new StudentEnrollmentDAL();
                return studentEnrollmentDAL.GetEnrollmentByDiscipline(studentEnrollment.DisciplineId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}
